package com.sz.biz.app.web;

import com.sz.biz.common.constants.MessagesCodes;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.base.i18n.I18nMessages;
import com.sz.common.base.logger.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Function: BaseController <br>
 * Author: hpeng.wang <br>
 * Date: 2016-10-01 14:11:00
 */
public class BaseController {

    /**
     * 日志记录器
     */
    protected Logger logger = Logger.getLogger(this.getClass());


    public static void logBefore(Logger logger, String interfaceName) {

        logger.info("");
        logger.info("start");
        logger.info(interfaceName);
    }

    public static void logAfter(Logger logger) {
        logger.info("end");
        logger.info("");
    }


    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();

        return request;
    }

    /**
     * i18N相关的操作  start
     */
    @Autowired
    protected I18nMessages messageSource;

    /**
     * @param moduleKey --模块国际化信息key
     * @return 包含添加成功的result dto
     */
    protected ResultDto getSaveSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_SAVE_SUCCESS, names));
    }

    /**
     * @param moduleKey --模块国际化信息key
     * @return 包含生成成功的result dto
     */
    protected ResultDto getGenerativeSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_GENERATIVE_SUCCESS, names));
    }
    /**
     * @return 包含添加成功的result dto
     */
    protected ResultDto getSaveI18nSuccessDto() {
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_SAVE_SIMPLE_SUCCESS, new String[0]));
    }

    /**
     * @param moduleKey --模块国际化信息key
     * @return 包含修改成功的result dto
     */
    protected ResultDto getUpdateSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_UPDATE_SUCCESS, names));
    }


    /**
     * @param moduleKey --模块国际化信息key
     * @return 包含修改成功的result dto
     */
    protected ResultDto getSaveBaseSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_SAVE_BASE_SUCCESS, names));
    }

    /**
     * @param moduleKey --模块国际化信息key
     * @return 包含删除成功的result dto
     */
    protected ResultDto getDeleteSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_DELETE_SUCCESS, names));
    }


    protected ResultDto getSubmitSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_SUBMIT_SUCCESS, names));
    }
    protected ResultDto getOrderSubmitSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_ORDER_SUBMIT_SUCCESS, names));
    }
    protected ResultDto getConfirmationSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_CONFIRMATION_SUCCESS, names));
    }
    protected ResultDto getDeliverySuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_DERLIVERY_SUCCESS, names));
    }

    protected ResultDto getOperateSuccessDto(String code) {
        return new ResultDto(0, messageSource.getMessage(code, null));
    }
    protected ResultDto getOperateSuccessDto(String code,String []param) {
        return new ResultDto(0, messageSource.getMessage(code, param));
    }

    protected ResultDto getOperatePaidSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_PAID_SUCCESS, names));
    }

    protected ResultDto getChargeoffSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_CHARGEOFF_SUCCESS, names));
    }

    protected ResultDto getAuditSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_AUDIT_SUCCESS, names));
    }
    protected ResultDto getOperationSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_OPERATE_SUCCESS, names));
    }

    protected ResultDto getTaskStartSuccessDto(String taskCode) {
        String[] names = new String[]{taskCode};
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_TASK_START_SUCCESS, names));
    }

    protected ResultDto getBillingAdjustmentSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_ADJUSTMENT_SUCCESS, names));
    }

    protected ResultDto getAuditAndEnableSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_AUDIT_ENABLE_SUCCESS, names));
    }

    protected ResultDto getOnlineSuccessDto() {
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_ONLINE_SUCCESS, null));
    }

    protected ResultDto getOfflineSuccessDto() {
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_OFFLINE_SUCCESS, null));
    }

    protected ResultDto getRejectSuccessDto() {
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_REJECT_SUCCESS, null));
    }

    protected ResultDto saveDraftSuccessDto() {
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_SAVEDRAFT_SUCCESS, null));
    }

    protected ResultDto setPayPwdSuccessDto(){
        return new ResultDto(0,messageSource.getMessage(MessagesCodes.MODULE_PAY_PASSWORD_SET_SUCCESS,null));
    }

    protected ResultDto getAcceptSuccessDto(String moduleKey) {
        String[] names = getModuleName(moduleKey);
        return new ResultDto(0, messageSource.getMessage(MessagesCodes.OPERATION_ORDER_ACCEPT_SUCCESS, names));
    }

    /**
     * @param moduleKey -- module key
     * @return module 字符
     */
    private String[] getModuleName(String moduleKey) {
        String[] moduleNames = null;
        if (StringUtils.hasLength(moduleKey)) {
            String moduleName = messageSource.getMessage(moduleKey, null);
            moduleNames = new String[]{moduleName};
        }
        return moduleNames;
    }

    /** i18N相关的操作   end */

    /**
     * Biz exception 转换为ResultDto
     *
     * @param ex -- BizException
     * @return --ResultDto
     */
    protected ResultDto convertBizException(BizException ex) {
        ResultDto dto = new ResultDto();
        if (ex != null) {
            dto.setErrorCode(ex.getErrorCode());
            dto.setMsg(ex.getMsg());
        }
        return dto;
    }
}
